package com.ciwong.epaper.modules.me.ui;

import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.event.EventBusFactory;
import com.ciwong.epaper.modules.epaper.adapter.CatalogAdapter;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.util.DialogUtil;
import com.ciwong.epaper.util.download.DownLoad;
import com.ciwong.epaper.util.download.DownLoadDB;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.epaper.util.download.DownloadStatus;
import com.ciwong.eventbus.EventBus;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ThreadTask;

import java.util.ArrayList;
import java.util.List;

/**
 * 正在下载资源界面
 * Created by res-jianpingliu on 2015/7/27.
 */
public class LoadingFileActivity extends BaseActivity {
    private ListView mLoadingLv;

    private CatalogAdapter mDirectoryAdapter;

    private List<DownLoadInfo> mList;

    private LinearLayout llEmpty;

    /**
     * 设置activity的布局文件
     *
     * @return 布局文件的resId
     */
    @Override
    protected int setView() {
        return R.layout.activity_loading_files;
    }

    /**
     * 获取界面元素
     */
    @Override
    protected void findViews() {
        mLoadingLv = (ListView) findViewById(R.id.loading_files_lv);
        llEmpty = (LinearLayout) findViewById(R.id.ll_empty);
        llEmpty.setVisibility(View.GONE);
    }

    /**
     * 设置事件
     */
    @Override
    protected void initEvent() {
        mLoadingLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                DownLoadInfo info = mList.get(position
                        - mLoadingLv.getHeaderViewsCount());
                switch (info.getStatus()) {
                    case DownloadStatus.STATUS_NORMAL:
                    case DownloadStatus.STATUS_FAILED:
                    case DownloadStatus.STATUS_FAILED_SDISFULL:
                    case DownloadStatus.STATUS_FAILED_UNZIP:
                        if (DialogUtil
                                .sdIsFullForDownload(LoadingFileActivity.this, info)) {
                            DialogUtil.showSdFullDialog(
                                    LoadingFileActivity.this,
                                    info.getSize());
                            return;
                        }
                        DownLoad.getInstance().addTask(info);
                        break;
                    case DownloadStatus.STATUS_LOADING:
                        DownLoad.getInstance().pauseTask(info);
                        break;
                    case DownloadStatus.STATUS_DOWNLOADING:
                        DownLoad.getInstance().pauseTask(info);
                        break;
                    case DownloadStatus.STATUS_PAUSE:
                        DownLoad.getInstance().resumeTask(info);
                        break;
                    case DownloadStatus.STATUS_UPZIPING:
                        // DownLoad.getInstance().addTask(info);
                        break;
                    case DownloadStatus.STATUS_COMPLETED:
                        break;
                }
            }
        });
    }

    /**
     * 对象的初始化工作
     */
    @Override
    protected void init() {
        mList = new ArrayList<DownLoadInfo>();
        EventBus.getDownLoadInstance().register(this);
        setTitleText(R.string.loading_flies);

        ThreadTask.getInstance().executorOtherThread(new Runnable() {
            @Override
            public void run() {
                mList = DownLoadDB.queryUnCompletedList();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showLlEmpty();
                        mDirectoryAdapter = new CatalogAdapter(
                                LoadingFileActivity.this, mList, false,
                                CatalogueInfo.LIST_DOWNLOADING);
                        mLoadingLv.setAdapter(mDirectoryAdapter);
                    }
                });
            }
        }, ThreadTask.ThreadPeriod.PERIOD_HIGHT);
    }

    public void onEventMainThread(EventBusFactory.DownloadEvent item) {
        DownLoadInfo info = item.getInfo();
        if (mList.contains(info)) {
            DownLoadInfo mCurrentInfo = mList.get(mList.indexOf(info));
            mCurrentInfo.setStatus(info.getStatus());
            mCurrentInfo.setProgress(info.getProgress());
            mDirectoryAdapter.notifyDataSetChanged();
        }
    }

    public void showLlEmpty() {
        if (mList == null || mList.size() == 0) {
            this.llEmpty.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 加载数据
     */
    @Override
    protected void loadData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDownLoadInstance().unregister(this);
    }
}
